home *** CD-ROM | disk | FTP | other *** search
- Path: navet.enator.se!usenet
- From: Hardy Henneberg <hh@enator.dk>
- Newsgroups: comp.lang.c++,fj.lang.c++
- Subject: Re: [Q]When temporary object be created?
- Date: 17 Apr 1996 09:07:16 GMT
- Organization: Enator AB
- Message-ID: <4l2cc4$djl@navet.enator.se>
- NNTP-Posting-Host: 193.162.31.53
-
- matusita@hv.epson.co.jp (Hiroshi-MATSUSHITA) writes:
- > When a temporary object will be created?
- >
- > Suppose, I create a class named PERSON that takes a name
- > in its constructor like following:
- >
- > class PERSON {
- > public:
- > PERSON(const STRING& nm);
- > ...
- > private:
- > STRING name;
- > };
- >
- > Then, when I create an instance of PERSON using raw string,
- > I may write like this:
- >
- > PERSON nomo("Hideo Nomo");
- >
- > STRING class, in this case, is not a smart pointer, but it
- > creates a character buffer newly using 'new' operator in its
- > constructor and delete it in its destructor.
- >
- > In this case, will a temporary string instance be created?
-
- Yes, Your PERSON constructor takes a STRING as parameter.
- You initialize a PERSON object with a const char*, so the compiler
- to find a PERSON constructor with a const char* as parameter. Because
- that doesn't exits, it will use the rules for function matching to find
- a constructor which matches the call. One of these rules says, there
- is a match if the const char* can be typeconverted into a STRING with a
- typeconverting function. I guess the STRING class must have a typecast
- constructor (a constructor with 1 parameter), with a const char* parameter.
- So, this string constructor must be called resulting in a temporary STRING
- object.
-
- > Does it depend on how to create the constructor?
- I dont' understand the question.
-
- > Does it depend on a compiler? (undefined in C++ spec.?)
- No.
-
- >
- > If I initialize the private member 'name' using initializing
- > list like following, can I avoid to create a temporary object?
- >
- > PERSON::PERSON(const STRING& nm) : name(nm) {...}
-
- No, the implementation of the PERSON constructor and the initialisation
- of the PERSON object will normally be in two seperate compilated modules
- with no possibility for the compiler to optimize.
-
- >
- > Why I want to know this is because I don't want to take extra time
- > and memory space even if it's temporary.
- > I'm pazzling my brain which I'd better to take between reference
- > approach described above and pointer approach like following:
- >
- > class PERSON {
- > public:
- > PERSON(const char* nm);
- > ...
- > private:
- > STRING name;
- > };
- >
- > In this case (pointer approach), *no* temporary object will be
- > created. Also, this constructor can accept STRING object, because
- > STRING class has a conversion operator to "const char*", and it
- > works implicitly. (Does this feature bring another argue?)
- >
- This is a possible solution, and you can even have both constructors.
- Then the function matching rules will tell the compiler to use
- the constructor with a const char* in your case, because it's
- an exact match.
-
- But remember, that when you define a new constructor with 1 parameter you
- also make a new hole in the type control, which may let errors pass
- the compiler.
-
- If you are worried about memory space a solution could be
- to use an intelligent STRING class, which only uses one buffer for
- all strings with the same content.
-
- If intialising PERSON with a const char* really is a performance issue,
- I'm sure there is also another solution for that.
-
- I think using char* all over in a program should be avoided - it's much
- better to use the STRING class.
-
- I'm also against misusing type cast constructors.
-
- My experience from working 8 years in C++ is that char* and type
- cast constructors cause a lot of problems.
-
- cheers,
- Hardy Henneberg
- ENATOR, Denmark
-
- >
- > (If this is a FAQ, please forgive me. Then I appreciate
- > if you let me know.)
- >
- > Thank you,
- >
- > --Hiroshi Matsushita @ Seiko Epson Corp. (matusita@hv.epson.co.jp)
- >
-
-